package com.hjy.scheduled.config.redis.config;

import com.hjy.scheduled.config.redis.BaseRedisCacheImpl;
import com.hjy.scheduled.config.redis.CacheClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * redis的连接池属性
 * CacheClient，redis生成
 */
@Configuration
@EnableAutoConfiguration
public class RedisCacheConfig {

    @Autowired
    private JdkSerializationRedisSerializer valueSerializer;

    @Autowired
    private StringRedisSerializer keySerializer;

    @Autowired
    private RedisConfig redisConfig;

    @Bean
    public JedisPoolConfig getRedisConfig() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(redisConfig.getMaxTotal());
        config.setMaxIdle(redisConfig.getMaxIdle());
        config.setMinIdle(redisConfig.getMinIdle());
        config.setMaxWaitMillis(redisConfig.getMaxWaitMillis());
        return config;
    }

    /**
     * 工厂类
     * @return
     */
    @Bean
    public JedisConnectionFactory getConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        JedisPoolConfig config = getRedisConfig();
        factory.setPoolConfig(config);
        factory.setHostName(redisConfig.getHost());
        factory.setPort(redisConfig.getPort());
        factory.setDatabase(redisConfig.getDatabase());//设置使用Redis的第几个db
        if (StringUtils.isNotBlank(redisConfig.getPassword())) {
            factory.setPassword(redisConfig.getPassword().trim());
        }
        return factory;
    }

    /**
     * RedisTemplate模板
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
        template.setKeySerializer(keySerializer);
        template.setValueSerializer(valueSerializer);
        template.setDefaultSerializer(valueSerializer);
        template.setHashValueSerializer(valueSerializer);
        template.setConnectionFactory(factory);
        return template;
    }

    @Bean
    public StringRedisSerializer stringRedisSerializer() {
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        return stringRedisSerializer;
    }

    @Bean
    public JdkSerializationRedisSerializer jdkRedisSerializer() {
        JdkSerializationRedisSerializer jdkRedisSerializer = new JdkSerializationRedisSerializer();
        return jdkRedisSerializer;
    }

    /**
     * CacheClient生成bean
     * @return
     */
    @Bean
    public CacheClient redisClientFactory() {
        CacheClient cc = new BaseRedisCacheImpl();
        return cc;
    }

}
